import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';

class FlexibleIconText extends StatelessWidget {
  final List<String>? textList;
  final String? text;
  final String? secondText;
  final Widget? icon;
  final TextStyle? textStyle;
  final double? iconTextPadding;

  FlexibleIconText({
    this.textList,
    this.text,
    this.secondText,
    this.icon,
    this.textStyle,
    this.iconTextPadding,
  });

  @override
  Widget build(BuildContext context) {
    return buildTextIconWidget();
  }

  Widget buildTextIconWidget() {
    return Visibility(
      visible: (text != null && text!.isNotEmpty) || (secondText != null && secondText!.isNotEmpty) || (textList != null && textList!.isNotEmpty),
      child: Row(
        children: [
          icon ?? Container(),
          SizedBox(
            width: iconTextPadding,
          ),
          Flexible(
            child: RichText(
              text: TextSpan(
                style: textStyle ??
                    TextStyle(
                      color: Colours.color_6f7082,
                      fontSize: 12.sp,
                    ),
                children: getTextSpan(),
              ),
            ),
          ),
        ],
      ),
    );
  }

  List<InlineSpan> getTextSpan() {
    List<String> list = [];
    List<InlineSpan> spanList = [];
    if (text != null && text!.isNotEmpty) {
      list.add(text!);
    }
    if (secondText != null && secondText!.isNotEmpty) {
      list.add(secondText!);
    }
    if (textList != null && textList!.isNotEmpty) {
      list.addAll(textList!);
    }
    if (list.isNotEmpty) {
      for (int i = 0; i < list.length; i++) {
        String name = list[i];
        if (name.isNotEmpty) {
          if (i == list.length - 1) {
            spanList.add(TextSpan(text: name));
          } else {
            spanList.add(TextSpan(text: name));
            spanList.add(
              TextSpan(
                text: "  l  ",
                style: TextStyle(color: Colours.color_e7e7ea, fontSize: 12.sp),
              ),
            );
          }
        }
      }
    }
    return spanList;
  }
}
